/*
 * HoangTN
 * servlet controller for pay01010.jsp
 */
package vn.edu.fpt.xml.hrm.controller.payroll;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import vn.edu.fpt.xml.hrm.controller.AbstractController;
import vn.edu.fpt.xml.hrm.controller.timesheet.NewActivitiesUtil;
import vn.edu.fpt.xml.hrm.pojo.Payroll;

/**
 *
 * @author HoangTN
 */
public class SearchPayrollController extends AbstractController {

    HttpServletRequest request = this.getRequest();
    HttpServletResponse response = this.getResponse();
    private final String homePage = "jsp/pay01010.jsp";
    private final String errorPage = "jsp/error.jsp";

    @Override
    protected String execute() {


        String webPath = getServletContext().getRealPath("/");
        String filePath = webPath + "WEB-INF/payroll.xml";

        Document doc = NewActivitiesUtil.createDOMTree(filePath);

        String fromYear = request.getParameter("fromYear");
        String fromMonth = request.getParameter("fromMonth");
        String fromDay = request.getParameter("fromDay");
        String toYear = request.getParameter("toYear");
        String toMonth = request.getParameter("toMonth");
        String toDay = request.getParameter("toDay");

        ////payroll[number(translate(normalize-space(payrollDate),'/','')) >= 20121118
        //and number(translate(normalize-space(payrollDate),'/','')) <= 20121119]

        String fromDate = fromYear + fromMonth + fromDay;
        String toDate = toYear + toMonth + toDay;

        String url = errorPage;
        String action = request.getParameter("action");

        if (action.equals("View")) {
            try {
                XPathFactory xpf = XPathFactory.newInstance();
                XPath xpath = xpf.newXPath();
                String exp = "//payroll[payrollDate >= " + fromDate + " and payrollDate <= " + toDate + "]";
                NodeList result = (NodeList) xpath.evaluate(exp, doc, XPathConstants.NODESET);

                List list = addToList(result);
                if (!list.isEmpty()) {
                    HttpSession session = request.getSession();
                    session.setAttribute("result", list);
                    url = homePage;
                }

            } catch (XPathExpressionException ex) {
                Logger.getLogger(SearchPayrollController.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return url;
    }

    private List addToList(NodeList nodeList) {
        List list = null;
        int a = 0,
                b = 0,
                c = 0,
                d = 0;

        for (int i = 0; i < nodeList.getLength(); i++) {
            if (nodeList.item(i).getNodeName().equals("payroll")) {
                Node payrollNode = nodeList.item(i);

                Payroll payroll = new Payroll();
                payroll.setPayrollID(payrollNode.getAttributes().getNamedItem("payrollID").
                        getNodeValue());

                NodeList payrollEls = payrollNode.getChildNodes();
                for (int j = 0; j < payrollEls.getLength(); j++) {
                    Node el = payrollEls.item(j);
                    if (el.getNodeName().equals("employeeID")) {
                        payroll.setEmployeeID(el.getTextContent().trim());
                        a = 1;
                    }
                    if (el.getNodeName().equals("payrollDate")) {
                        payroll.setPayrollDate(el.getTextContent().trim());
                        b = 1;
                    }
                    if (el.getNodeName().equals("amount")) {
                        payroll.setAmount(Float.parseFloat(el.getTextContent().trim()));
                        c = 1;
                    }
                    if (el.getNodeName().equals("payrollType")) {
                        payroll.setPayrollType(el.getTextContent().trim());
                        d = 1;
                    }
                    if ((a * b * c * d) == 1) {
                        list.add(payroll);
                    }
                }
            }
        }
        return list;
    }
}
